package com.example.electricbicycle.dao;


import com.example.electricbicycle.po.PurchaseOrder;
import utils.DBUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PurchaseOrderDaoImpl implements PurchaseOrderDao {
    // 审核订单
    public boolean approveOrder(int orderId) {
        String sql = "UPDATE purchase_order SET status = 1 WHERE id = ? AND status = 0";
        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, orderId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 获取待审核订单
    public List<PurchaseOrder> getPendingOrders(int page, int pageSize) {
        List<PurchaseOrder> orders = new ArrayList<>();
        String sql = "SELECT * FROM purchase_order WHERE status = 0 ORDER BY create_time DESC LIMIT ? OFFSET ?";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, pageSize);
            pstmt.setInt(2, (page - 1) * pageSize);

            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    orders.add(new PurchaseOrder(
                            rs.getInt("id"),
                            rs.getInt("user_id"),
                            rs.getInt("v_id"),
                            rs.getInt("quantity"),
                            rs.getByte("status"),
                            rs.getDate("create_time")
                    ));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }

    public int getTotalPendingOrders() {
        String sql = "SELECT COUNT(*) FROM purchase_order WHERE status = 0";
        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public PurchaseOrder getOrderById(int orderId) {
        String sql = "SELECT * FROM purchase_order WHERE id = ?";
        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, orderId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return new PurchaseOrder(
                            rs.getInt("id"),
                            rs.getInt("user_id"),
                            rs.getInt("v_id"),
                            rs.getInt("quantity"),
                            rs.getByte("status"),
                            rs.getDate("create_time")
                    );
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}